<html>
<head><meta charset="utf-8"><title>MIR-level inlining · t-compiler/wg-mir-opt · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/index.html">t-compiler/wg-mir-opt</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html">MIR-level inlining</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="210604884"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210604884" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210604884">(Sep 19 2020 at 08:48)</a>:</h4>
<div class="codehilite"><pre><span></span><code><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="s">&quot;Hello world!&quot;</span><span class="p">).</span><span class="n">map</span><span class="p">(</span><span class="o">|</span><span class="n">s</span><span class="o">|</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;{}&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">s</span><span class="p">);</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>I wonder if there's some configuration that can do the inlining at MIR level? I tried </p>
<div class="codehilite"><pre><span></span><code>rustc +nightly -O --emit mir src/main.rs -Z mir-opt-level=2
</code></pre></div>


<p>but the mir generated still contains the <code>Option::map</code> code.</p>



<a name="210607222"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210607222" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210607222">(Sep 19 2020 at 09:51)</a>:</h4>
<p>Does it contain a call or the inlined body?</p>



<a name="210608407"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210608407" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210608407">(Sep 19 2020 at 10:26)</a>:</h4>
<p>it contains a call.</p>



<a name="210609386"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210609386" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210609386">(Sep 19 2020 at 10:55)</a>:</h4>
<p>curious, but I guess our inliner is pretty unusable anyway, so not too surprising</p>



<a name="210609836"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210609836" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210609836">(Sep 19 2020 at 11:09)</a>:</h4>
<p>If there's anything a beginner (i) can do, i'm willing to help. Personally i think all inlining should happen before monomorphization in an ideal world...</p>



<a name="210609953"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210609953" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210609953">(Sep 19 2020 at 11:12)</a>:</h4>
<p>ideally, yes. The current status is twofold: <a href="https://github.com/rust-lang/rust/pull/68828">https://github.com/rust-lang/rust/pull/68828</a> is needed to make inlining work regardless of function declaration order within a crate. The second thing is that we need a non-heuristic algorithm for deciding when to inline a function</p>



<a name="210609966"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210609966" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Notification Bot <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210609966">(Sep 19 2020 at 11:13)</a>:</h4>
<p>This topic was moved here from <a class="stream-topic" data-stream-id="131828" href="/#narrow/stream/131828-t-compiler/topic/MIR-level.20inlining">#t-compiler &gt; MIR-level inlining</a> by <span class="user-mention silent" data-user-id="124288">oli</span></p>



<a name="210610617"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210610617" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210610617">(Sep 19 2020 at 11:32)</a>:</h4>
<p>the first is already implemented but there're some perf impacts?<br>
the second, maybe we can steal one from llvm? i'm also curious why do we need a non-heuristic one?</p>



<a name="210611118"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210611118" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210611118">(Sep 19 2020 at 11:45)</a>:</h4>
<p>uff... for the second there was a huge discussion that I don't have a good summary of. Maybe <span class="user-mention" data-user-id="125250">@Wesley Wiser</span> remembers where they discussed that?</p>



<a name="210611178"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210611178" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210611178">(Sep 19 2020 at 11:46)</a>:</h4>
<p>Uh, I wasn't sure why we couldn't use a heuristic either :)</p>



<a name="210611195"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210611195" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210611195">(Sep 19 2020 at 11:47)</a>:</h4>
<p>Maybe I'm misinterpreting what you mean by heuristic. AIUI LLVM just uses a set of heuristics too.</p>



<a name="210611198"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210611198" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210611198">(Sep 19 2020 at 11:47)</a>:</h4>
<p>I thought <span class="user-mention" data-user-id="119009">@eddyb</span> came up with a plan for a deterministic inliner?</p>



<a name="210611199"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210611199" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210611199">(Sep 19 2020 at 11:47)</a>:</h4>
<p>The current set is actually based on those ones as I recall.</p>



<a name="210611204"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210611204" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210611204">(Sep 19 2020 at 11:47)</a>:</h4>
<p>Oh, is this because of the query system?</p>



<a name="210611205"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210611205" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210611205">(Sep 19 2020 at 11:47)</a>:</h4>
<p>no</p>



<a name="210611213"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210611213" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210611213">(Sep 19 2020 at 11:47)</a>:</h4>
<p>maybe that was for mir-opt-level=1</p>



<a name="210611260"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210611260" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210611260">(Sep 19 2020 at 11:48)</a>:</h4>
<p>I think the idea was to inline whenever we know that inlining is definitely an improvement</p>



<a name="210611265"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210611265" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210611265">(Sep 19 2020 at 11:48)</a>:</h4>
<p>e.g. <code>Vec::len</code></p>



<a name="210611269"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210611269" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210611269">(Sep 19 2020 at 11:49)</a>:</h4>
<p>For mir-opt-level=1, that's probably what we want to do even though it's very restrictive</p>



<a name="210611274"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210611274" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210611274">(Sep 19 2020 at 11:49)</a>:</h4>
<p>and then activate the heuristic at mir-opt-level=2 or wherever we allow expensive opts?</p>



<a name="210611337"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210611337" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210611337">(Sep 19 2020 at 11:50)</a>:</h4>
<p>Yeah, that makes sense</p>



<a name="210611339"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210611339" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210611339">(Sep 19 2020 at 11:50)</a>:</h4>
<p>anyway... I should probably revive my inlining cycle PR and see if it's still a perf regression if we also turn on inlining</p>



<a name="210611343"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210611343" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210611343">(Sep 19 2020 at 11:51)</a>:</h4>
<p>since right now all it does is extra work <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span></p>



<a name="210611402"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210611402" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210611402">(Sep 19 2020 at 11:52)</a>:</h4>
<p>I'd like to give some context here:</p>
<div class="codehilite"><pre><span></span><code>  Lines           Copies         Function name
  -----           ------         -------------
  7668776 (100%)  240632 (100%)  (TOTAL)
   223241 (2.9%)    3437 (1.4%)  core::option::Option&lt;T&gt;::map
   185588 (2.4%)   13396 (5.6%)  core::ptr::drop_in_place
</code></pre></div>


<p>This is cargo-llvm-lines result on <code>rustc_middle</code>.</p>



<a name="210611419"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210611419" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210611419">(Sep 19 2020 at 11:53)</a>:</h4>
<p>no clue why the current heuristic doesn't inline <code>Option::map</code></p>



<a name="210611471"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210611471" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210611471">(Sep 19 2020 at 11:54)</a>:</h4>
<p>maybe it already inlined the closure call and then the body is too large to get inlined?</p>



<a name="210611533"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210611533" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210611533">(Sep 19 2020 at 11:56)</a>:</h4>
<div class="codehilite"><pre><span></span><code>fn main() -&gt; () {
    let mut _0: ();                      // return place in scope 0 at src/main.rs:1:11: 1:11
    let mut _1: std::option::Option&lt;()&gt;; // in scope 0 at src/main.rs:2:13: 2:66
    let mut _2: std::option::Option&lt;&amp;str&gt;; // in scope 0 at src/main.rs:2:13: 2:33
    let mut _3: [closure@src/main.rs:2:38: 2:64]; // in scope 0 at src/main.rs:2:38: 2:64
    scope 1 {
    }

    bb0: {
        StorageLive(_1);                 // scope 0 at src/main.rs:2:13: 2:66
        StorageLive(_2);                 // scope 0 at src/main.rs:2:13: 2:33
        ((_2 as Some).0: &amp;str) = const &quot;Hello world!&quot;; // scope 0 at src/main.rs:2:13: 2:33
                                         // ty::Const
                                         // + ty: &amp;str
                                         // + val: Value(Slice { data: Allocation { bytes: [72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [4095], len: Size { raw: 12 } }, size: Size { raw: 12 }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 12 })
                                         // mir::Constant
                                         // + span: src/main.rs:2:18: 2:32
                                         // + literal: Const { ty: &amp;str, val: Value(Slice { data: Allocation { bytes: [72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [4095], len: Size { raw: 12 } }, size: Size { raw: 12 }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 12 }) }
        discriminant(_2) = 1;            // scope 0 at src/main.rs:2:13: 2:33
        StorageLive(_3);                 // scope 0 at src/main.rs:2:38: 2:64
        _1 = Option::&lt;&amp;str&gt;::map::&lt;(), [closure@src/main.rs:2:38: 2:64]&gt;(move _2, move _3) -&gt; bb1; // scope 0 at src/main.rs:2:13: 2:66
                                         // mir::Constant
                                         // + span: src/main.rs:2:34: 2:37
                                         // + literal: Const { ty: fn(std::option::Option&lt;&amp;str&gt;, [closure@src/main.rs:2:38: 2:64]) -&gt; std::option::Option&lt;()&gt; {std::option::Option::&lt;&amp;str&gt;::map::&lt;(), [closure@src/main.rs:2:38: 2:64]&gt;}, val: Value(Scalar(&lt;ZST&gt;)) }
    }

    bb1: {
        StorageDead(_3);                 // scope 0 at src/main.rs:2:65: 2:66
        StorageDead(_2);                 // scope 0 at src/main.rs:2:65: 2:66
        StorageDead(_1);                 // scope 0 at src/main.rs:2:66: 2:67
        _0 = const ();                   // scope 0 at src/main.rs:1:11: 3:2
        return;                          // scope 0 at src/main.rs:3:2: 3:2
    }
}
</code></pre></div>



<a name="210611540"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210611540" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210611540">(Sep 19 2020 at 11:56)</a>:</h4>
<p>This is mir main function</p>



<a name="210611542"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210611542" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210611542">(Sep 19 2020 at 11:57)</a>:</h4>
<p>code is this:</p>
<div class="codehilite"><pre><span></span><code>fn main() {
    let _ = Some(&quot;Hello world!&quot;).map(|s| { println!(&quot;{}&quot;, s); } );
}
</code></pre></div>



<a name="210611735"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210611735" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210611735">(Sep 19 2020 at 12:02)</a>:</h4>
<p>I also wonder if it makes sense to have a separate program maybe called <code>mir-opt</code> to debug the mir optimization process directly. It's a pain to have to build whole rustc with any tiny change...</p>



<a name="210612038"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210612038" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210612038">(Sep 19 2020 at 12:11)</a>:</h4>
<p>what I mean is that the body of <code>Option::&lt;&amp;str&gt;::map::&lt;(), [closure@src/main.rs:2:38: 2:64]&gt;</code> may have inlined the closure already. Thus the body of that function is really large and doesn't get inlined into <code>main</code></p>



<a name="210612077"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210612077" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210612077">(Sep 19 2020 at 12:12)</a>:</h4>
<p>try using a really simple closure</p>



<a name="210612144"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210612144" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210612144">(Sep 19 2020 at 12:13)</a>:</h4>
<p><code>map</code> is generic though, how could it inline the closure?</p>



<a name="210612155"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210612155" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210612155">(Sep 19 2020 at 12:13)</a>:</h4>
<p>well, the specific monomorphic instance could inline... oh we don't do that lol</p>



<a name="210612163"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210612163" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210612163">(Sep 19 2020 at 12:14)</a>:</h4>
<p>sorry idk where my mind went there</p>



<a name="210612166"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210612166" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210612166">(Sep 19 2020 at 12:14)</a>:</h4>
<p>ok</p>



<a name="210612207"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210612207" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210612207">(Sep 19 2020 at 12:14)</a>:</h4>
<p>so back to the basics... I guess you can use a debug rustc and enable logging to see what the inliner says about the fact that it's not inlining stuff</p>



<a name="210612208"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210612208" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210612208">(Sep 19 2020 at 12:14)</a>:</h4>
<p>I know that inside one crate we do some weird DefId comparisons which could prohibit this, but since <code>map</code> comes from libcore I don't see any reason it shouldn't get inlined</p>



<a name="210612270"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210612270" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210612270">(Sep 19 2020 at 12:16)</a>:</h4>
<p><span class="user-mention" data-user-id="124288">@oli</span> Thanks for the hints! what <code>RUSTC_LOG</code> setting should i use here? I'll give it a try tonight. (After i build a debug rustc...)</p>



<a name="210612278"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210612278" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210612278">(Sep 19 2020 at 12:16)</a>:</h4>
<p><code>rustc_mir::transform::inline</code></p>



<a name="210614154"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210614154" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210614154">(Sep 19 2020 at 13:05)</a>:</h4>
<p><span class="user-mention silent" data-user-id="124288">oli</span> <a href="#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/MIR-level.20inlining/near/210612155">said</a>:</p>
<blockquote>
<p>well, the specific monomorphic instance could inline... oh we don't do that lol</p>
</blockquote>
<p>LLVM could be though since it does see the monmorphized <code>Option::map()</code> function. I think you're probably right that it's inlining the closure into <code>map</code> first and that makes it too big to inline into the callsite.</p>



<a name="210616725"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210616725" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210616725">(Sep 19 2020 at 14:03)</a>:</h4>
<div class="codehilite"><pre><span></span><code>Sep 19 21:59:13.564 DEBUG rustc_mir::transform::inline: should_inline(CallSite { callee: DefId(2:5601 ~ core[93f7]::option[0]::{{impl}}[0]::map[0]), substs: [&amp;str, (), [closure@src\main.rs:2:31: 2:57]], bb: bb0, location: SourceInfo { span: src\main.rs:2:13: 2:58 (#0), scope: scope[0] } })
Sep 19 21:59:13.564 DEBUG rustc_mir::transform::inline:     final inline threshold = 100
Sep 19 21:59:13.564 DEBUG rustc_mir::transform::inline: NOT inlining CallSite { callee: DefId(2:5601 ~ core[93f7]::option[0]::{{impl}}[0]::map[0]), substs: [&amp;str, (), [closure@src\main.rs:2:31: 2:57]], bb: bb0, location: SourceInfo { span: src\main.rs:2:13: 2:58 (#0), scope: scope[0] } } [cost=222 &gt; threshold=100]
</code></pre></div>



<a name="210618833"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210618833" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210618833">(Sep 19 2020 at 14:48)</a>:</h4>
<p><span aria-label="laughter tears" class="emoji emoji-1f602" role="img" title="laughter tears">:laughter_tears:</span> </p>
<div class="codehilite"><pre><span></span><code><span class="w">            </span><span class="c1">// In debug builds, trigger a panic on overflow.</span>
<span class="w">            </span><span class="c1">// This should optimize completely out in release builds.</span>
<span class="w">            </span><span class="k">if</span><span class="w"> </span><span class="bp">Self</span>::<span class="n">forward_checked</span><span class="p">(</span><span class="n">start</span><span class="p">,</span><span class="w"> </span><span class="n">n</span><span class="p">).</span><span class="n">is_none</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="kd">let</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Add</span>::<span class="n">add</span><span class="p">(</span><span class="bp">Self</span>::<span class="n">MAX</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>with mir inlining this triggers const prop which will subsequently complain about the statically known overflow</p>



<a name="210619235"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210619235" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210619235">(Sep 19 2020 at 14:57)</a>:</h4>
<p><a href="#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/trivial.20inliner/near/206853915">https://rust-lang.zulipchat.com/#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/trivial.20inliner/near/206853915</a> <span aria-label="heart" class="emoji emoji-2764" role="img" title="heart">:heart:</span></p>



<a name="210619783"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210619783" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210619783">(Sep 19 2020 at 15:10)</a>:</h4>
<p>oh heh, I guess no solution there either</p>



<a name="210619791"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210619791" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210619791">(Sep 19 2020 at 15:10)</a>:</h4>
<div class="codehilite"><pre><span></span><code><span class="c1">// build-fail</span>
<span class="c1">// compile-flags: -Zmir-opt-level=2</span>

<span class="cp">#![deny(warnings)]</span><span class="w"></span>

<span class="cp">#[inline(always)]</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">add</span><span class="p">(</span><span class="n">x</span>: <span class="kt">u8</span><span class="p">,</span><span class="w"> </span><span class="n">y</span>: <span class="kt">u8</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">u8</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">x</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">y</span><span class="w"></span>
<span class="w">    </span><span class="c1">//~^ ERROR this arithmetic operation will overflow</span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">add</span><span class="p">(</span><span class="kt">u8</span>::<span class="n">MAX</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="210619798"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210619798" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210619798">(Sep 19 2020 at 15:10)</a>:</h4>
<p>repro... I guess I'm opening a new worktree</p>



<a name="210619807"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210619807" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210619807">(Sep 19 2020 at 15:10)</a>:</h4>
<p>damn all I wanted was to run perf</p>



<a name="210619811"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210619811" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210619811">(Sep 19 2020 at 15:10)</a>:</h4>
<p><span aria-label="rabbit" class="emoji emoji-1f407" role="img" title="rabbit">:rabbit:</span> <span aria-label="hole" class="emoji emoji-1f573" role="img" title="hole">:hole:</span> s it is</p>



<a name="210621001"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210621001" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210621001">(Sep 19 2020 at 15:39)</a>:</h4>
<p>So, for the original motivating case <code>Option::map</code>, on the MIR-level, currently MIR-level consider it too complex to be inlined. Cost  ~200 &gt; 100. From the user's side, i think it's fair to say that <code>#[inline]</code> on this particular API is not working as expected. And it should either be converted to a <code>#[inline(always)]</code> or be removed?</p>



<a name="210621171"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210621171" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210621171">(Sep 19 2020 at 15:42)</a>:</h4>
<p><code>#[inline]</code> is also forwarded to llvm</p>



<a name="210621219"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210621219" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210621219">(Sep 19 2020 at 15:42)</a>:</h4>
<p>so there it will still work</p>



<a name="210621239"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210621239" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210621239">(Sep 19 2020 at 15:42)</a>:</h4>
<p>in fact, that was the sole use case of it until MIR came along and some time later MIR inlining</p>



<a name="210621246"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210621246" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210621246">(Sep 19 2020 at 15:43)</a>:</h4>
<p>since MIR inlining is only an unstable thing, that is still the sole use from a stable perspective</p>



<a name="210621413"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level%20inlining/near/210621413" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR-level.20inlining.html#210621413">(Sep 19 2020 at 15:47)</a>:</h4>
<p>ok...</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>